package com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.add_cost_detail.service;

import com.hymjweb.demo.SpringAopUtil;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.add_cost_detail.bean.XtAirlinesAddCostDetailPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airlines.add_cost_detail.mapper.XtAirlinesAddCostDetailMapper;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class XtAirlinesAddCostDetailService extends BaseService {

    @Autowired
    private XtAirlinesAddCostDetailMapper xtAirlinesAddCostDetailMapper;

    public Object getPageList(Map map) {
        this.pageListInit(map);
        List list = xtAirlinesAddCostDetailMapper.findAll(map);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    public List<XtAirlinesAddCostDetailPojo> queryAddCostDetailListByAirlinesId(String airlinesId) throws Exception {
        if(DataTypeUtil.isEmptyStr(airlinesId)){
            throwException("根据航空公司ID查询航空附加费用参数错误!");
        }
        Map<String,Object> map = new HashMap();
        map.put("airlinesId",airlinesId);
        return xtAirlinesAddCostDetailMapper.findAll(map);
    }


    @Transactional(rollbackFor = {Exception.class})
    public int insertInit(XtAirlinesAddCostDetailPojo pojo) throws Exception{
        if(pojo == null){
            throwException("参数错误");
        }
        DataTypeUtil.isEmptyStrThenThrow(pojo.getAirlinesId(), SysConstant.XT_DIC_ERROR_MSG.no_id.getName());
        XtAirlinesAddCostDetailPojo xtAirlinesAddCostDetailPojo = findUK(pojo.getName(),pojo.getAirlinesId(),null);
        if(xtAirlinesAddCostDetailPojo != null){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.exist_name.getName());
        }
        Date currentDate = this.getSysTime();
        String czyId = this.getLoginCzyId();
        pojo.setId(getSysStrId());
        pojo.setCreateTime(currentDate);
        pojo.setCreateUserId(czyId);
        pojo.setUpdateTime(currentDate);
        pojo.setUpdateUserId(czyId);

        int affectRows = SpringAopUtil.getBean(this.getClass()).insert(pojo);
        return affectRows;
    }

    @Transactional(rollbackFor = {Exception.class})
    public int updateInit(XtAirlinesAddCostDetailPojo pojo) throws Exception{
        if(pojo == null){
            throwException("更新附加费参数错误");
        }
        DataTypeUtil.isEmptyStrThenThrow(pojo.getAirlinesId(), SysConstant.XT_DIC_ERROR_MSG.no_id.getName());
        pojo.setUpdateTime(this.getSysTime());
        pojo.setUpdateUserId(this.getLoginCzyId());
        //检查UK
        XtAirlinesAddCostDetailPojo xtAirlinesAddCostDetailPojo = findUK(pojo.getName(),pojo.getAirlinesId(),pojo.getId());
        if(xtAirlinesAddCostDetailPojo != null){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.exist_name.getName());
        }

        int affectRows = SpringAopUtil.getBean(this.getClass()).update(pojo);
        return affectRows;
    }

    public XtAirlinesAddCostDetailPojo findUK(String name,String airlinesId, String exceptId){
        XtAirlinesAddCostDetailPojo xtAirlinesAddCostDetailPojo = xtAirlinesAddCostDetailMapper.findUK(name,airlinesId,exceptId);
        return xtAirlinesAddCostDetailPojo;
    }

    public List<XtAirlinesAddCostDetailPojo> findByAirlinesId(String airlinesId){
        return xtAirlinesAddCostDetailMapper.findByAirlinesId(airlinesId);
    }

    @OperationLogger(oper = SysConstant.OPER_ADD, operTitle="附加费-添加" , operMemo = "", classType = XtAirlinesAddCostDetailPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(XtAirlinesAddCostDetailPojo pojo) throws Exception{
        if(pojo == null){
            throwException("保存附加费参数错误");
        }
        return xtAirlinesAddCostDetailMapper.insert(pojo);
    }


    @OperationLogger(oper = SysConstant.OPER_ADD, operTitle="附加费-更新" , operMemo = "", classType = XtAirlinesAddCostDetailPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int  update(XtAirlinesAddCostDetailPojo pojo) throws Exception{
        if(pojo == null){
            throwException("更新附加费参数错误");
        }
        return xtAirlinesAddCostDetailMapper.update(pojo);
    }

    @OperationLogger(oper = SysConstant.OPER_DELETE, operTitle="附加费-删除" , operMemo = "",  classType = XtAirlinesAddCostDetailPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int delete(String id) throws Exception{
        if(DataTypeUtil.isEmptyStr(id)){
            throwException("删除附加费参数错误");
        }
        int affectRows = xtAirlinesAddCostDetailMapper.delete(id);
        return affectRows;
    }


}
